"use strict";
var zeroFill = function (str, num) {
  if (num === void 0) { num = 3; }
  str = new Array(num + 1).join('0') + str;
  return str.substr(num * -1);
};
/**
 * 域名IP对象
 * @example
 *   www.aaaa.com, *.aaaa.com, 127.0.0.1, 127.*.0.1, 127.0.0.*
 * @class DomainIp
 */
var DomainIp = /** @class */ (function () {
  function DomainIp(domainIp) {
    this.domainIp = domainIp;
    /**
     * 类型 1为域名 2为IP 3为星号(*)网段
     *
     * @private
     * @type {number}
     * @memberof DomainIp
     */
    this.type = 0;
    this.isTopDomain = false;
    this.ipNumbers = ['', ''];
    this._domainIps = [];
    this.checkDomianIp();
  }
  /**
   * 检测
   *
   * @returns
   * @memberof DomainIp
   */
  DomainIp.prototype.checkDomianIp = function () {
    var strs = this._domainIps = this.domainIp.split(".");
    var nums;
    for (var i = 0; i < strs.length; i++) {
      var element = strs[i];
      if (element !== '*' && parseInt(element).toString() === 'NaN') {
        return this.type = 1;
      }
      nums = [zeroFill(element), zeroFill(element)];
      if (element === '*') {
        this.isTopDomain = true;
        nums = ['000', '255'];
        !this.type && (this.type = 3);
      }
      this.ipNumbers[0] += nums[0];
      this.ipNumbers[1] += nums[1];
      !this.type && (this.type = 2);
    }
  };
  DomainIp.prototype.isIn = function (ip) {
    var strs = ip.split(".");
    if (this.type === 1 && this.isTopDomain) {
      return strs[strs.length - 1] === this._domainIps[this._domainIps.length - 1] && strs[strs.length - 2] === this._domainIps[this._domainIps.length - 2];
    }
    if (this.type === 1) {
      return ip === this.domainIp;
    }
    var num = '';
    for (var i = 0; i < strs.length; i++) {
      num += zeroFill(strs[i]);
    }
    if (isNaN(Number(num)))
      return false;
    return this.ipNumbers[0] <= num && this.ipNumbers[1] >= num;
  };
  /**
   * domainIpType
   *
   * @returns type  1为域名 2为IP
   * @memberof DomainIp
   */
  DomainIp.prototype.domainIpType = function () {
    return this.type;
  };
  return DomainIp;
}());
module.exports = DomainIp;
